package cn.qf.shop.service.impl;

import cn.qf.shop.mapper.CommunityMapper;
import cn.qf.shop.pojo.entity.Community;
import cn.qf.shop.pojo.vo.RespResult;
import cn.qf.shop.service.CommunityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRES_NEW)
public class CommunityServiceImpl implements CommunityService {
    private Logger logger = LoggerFactory.getLogger(LoginUserServiceImpl.class);
    @Resource
    CommunityMapper communityMapper;

    @Override
    public RespResult addCommunity(Community community) throws RuntimeException{
        int value = communityMapper.add(community);
        RespResult result = new RespResult();
        if (value == 0){
            throw new RuntimeException("新增小区失败");
        }else{
            logger.toString();
            result.setData(RespResult.Code.SUCCESS);
        }
        return result;
    }

    @Override
    public RespResult delCommunity(int id) {
        int value = communityMapper.del(id);
        RespResult result = new RespResult();
        if (value == 0){
            throw new RuntimeException("删除小区失败");
        }else{
            logger.toString();
            result.setData(RespResult.Code.SUCCESS);
        }
        return result;
    }

    @Override
    public RespResult delCommunityBatch(List<String> idList) {
        for (Object o:idList) {
           int value = communityMapper.del(Integer.parseInt(o.toString()));
           if (value == 0){
               throw new RuntimeException("删除小区失败");
           }
        }
        RespResult result = new RespResult();
        logger.toString();
        result.setData(RespResult.Code.SUCCESS);
        return result;
    }

    @Override
    public RespResult updateCommunity(Community community) {
        int value = communityMapper.update(community);
        RespResult result = new RespResult();
        if (value == 0){
            throw new RuntimeException("修改小区信息失败");
        }else{
            logger.toString();
            result.setData(RespResult.Code.SUCCESS);
        }
        return result;
    }

    @Override
    public List<Community> checkAll() {
        List<Community> list = communityMapper.selectAll();
        return list;
    }

    @Override
    public List<Community> checkByTime(Date beginDate, Date endDate) throws RuntimeException{
        List<Community> list = communityMapper.selectByTime(beginDate,endDate);
        logger.toString();
        return list;
    }
}
